<template>
    <div class="article-lists">
        <el-card class="!border-none" shadow="never">

            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="用户姓名">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.keys"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.account"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <!-- <el-form-item label="群聊id">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.agent_group_id"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item> -->
                <el-form-item label="群聊">
                    <el-select class="w-80" v-model="queryParams.agent_group_id" placeholder="请选择" clearable filterable >
                        <el-option v-for="item in group_list" :key="item.group_id" :label="item.group.name" :value="item.group_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="审核状态">
                    <el-select class="w-[280px]" v-model="queryParams.status" clearable>
                        <el-option label="全部" :value="0" />
                        <el-option label="申请中" :value="1" />
                        <el-option label="审核通过待同意" :value="2" />
                        <el-option label="认证成功" :value="3" />
                        <el-option label="驳回" :value="4" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" shadow="never">
            <el-table size="large" v-loading="pager.loading" :data="pager.lists">
                <el-table-column label="序号(ID)" type="index" :index="indexAdd" min-width="100" />
                <el-table-column label="所属城市" min-width="120" prop="city_name">
                    <template #default="{row}">
                        <span v-if="row.city_name">{{ row.city_name }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="代理人账号" min-width="180" prop="account">
                    <template #default="{row}">
                        <!-- <router-link
                            v-perms="['user.agent/agentData']"
                            :to="{path: getRoutePath('user.agent/agentData'), query: {id: row.id}}"
                        >
                        <div class="flex justify-center items-center"  style="margin: 0;">
                            <el-link type="primary" class="mb-4" :underline="false"></el-link>
                        </div>
                            
                        </router-link> -->
                        <span class="txt">{{ row.account }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="代理人等级" min-width="140" prop="level">
                    <template #default="{row}">
                        <span v-if="row.level == 1">银牌代理人</span>
                        <span v-if="row.level == 2">金牌代理人</span>
                        <span v-if="row.level == 3">城市合伙人</span>
                    </template>
                </el-table-column>
                <el-table-column label="代理人用户名" min-width="180" prop="real_name"/>
                <el-table-column label="代理人渠道名" min-width="180" prop="channel_name">
                    <template #default="{row}">
                        <span v-if="row.channel_name">{{ row.channel_name }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="上级名称" min-width="180" prop="from_username">
                    <template #default="{row}">
                        <span v-if="row.from_username">{{ row.from_username }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="上级手机号" min-width="180" prop="from_account">
                    <template #default="{row}">
                        <span v-if="row.from_account">{{ row.from_account }}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="公司/个人业务类型" min-width="180" prop="business"/>
                <el-table-column label="报名费用" min-width="180" prop="pay_price"/>
                <el-table-column label="规模（人）" min-width="180" prop="user_count"/>
                <el-table-column label="直邀用户" min-width="180" prop="invite_count" sortable/>
                <el-table-column label="实名用户" min-width="180" prop="invite_auth_count" sortable/>
                <el-table-column label="直邀渠道" min-width="180" prop="invite_channel_count" sortable/>
                <el-table-column label="间邀用户" min-width="180" prop="indirect_user_count" sortable/>
                <el-table-column label="间邀渠道" min-width="180" prop="indirect_channel_count" sortable/>
                <el-table-column label="累积收益" min-width="180" prop="sum_price"/>
                <el-table-column label="合作单数" min-width="180" prop="order_count"/>

                <el-table-column label="操作" width="180" fixed="right">
                    <template #default="{ row }">
                        <el-button v-perms="['user.agent/agentData']" type="primary" link @click="handleSelectItem(row)">详情</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>
<script lang="ts" setup name="list">
import {applyList, authApply, groupList} from '@/api/agent'
import { usePaging } from '@/hooks/usePaging'
import router, { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'
import {useDictOptions} from "@/hooks/useDictOptions";

const group_list:any = ref([])
const getGroup = async () => {
    const res = await groupList({})
    console.log(res)
    group_list.value = res.list;
}
getGroup()

const queryParams = reactive({
    keys: '',
    account: '',
    status: '',
    agent_group_id: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: applyList,
    params: queryParams
})

const indexAdd = (index: number) => {
    const page = pager.page // 当前页码
    const pagesize = pager.size // 每页条数
    return index + 1 + (page - 1) * pagesize
}

const changeStatus = async (row:any) => {
    try {
        await feedback.confirm('是否审核通过？')
        await authApply({ 
            id: row.id,
            status: 2, 
        })
        feedback.msgSuccess('审核通过')
        getLists()
    } catch (error) {

    }
}

const handleSelectItem = (event: any) => {
    router.push({
        path: getRoutePath('user.agent/agentData'), query: { id: event.user_id }
    })
}

onActivated(() => {
    getLists()
})

getLists()
</script>
<style lang="scss" scoped>
.txt{
    color: var(--el-color-primary);
}
</style>